package com.ovopark.firecar.utils

import android.content.Context

/**
 * @author 陈涛(chentao@kedacom.com/tchen0707@163.com)
 *
 * 时间: 2014年4月8日 下午4:53:07
 *
 * 版本: V_1.0.0
 *
 * 描述: SharedPrefrence工具类 setParam存储getParam获取
 */
class SharedPreferencesUtils private constructor() {
    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param context
     * @param key
     * @param object
     */
    fun setParam(context: Context?, key: String?, `object`: Any?) {
        if (`object` != null && null != context) {
            val type = `object`.javaClass.simpleName
            val sp = context.getSharedPreferences(mPrefsName, Context.MODE_PRIVATE)
            val editor = sp.edit()
            if ("String" == type) {
                editor.putString(key, `object` as String?)
            } else if ("Integer" == type) {
                editor.putInt(key, (`object` as Int?)!!)
            } else if ("Boolean" == type) {
                editor.putBoolean(key, (`object` as Boolean?)!!)
            } else if ("Float" == type) {
                editor.putFloat(key, (`object` as Float?)!!)
            } else if ("Long" == type) {
                editor.putLong(key, (`object` as Long?)!!)
            }
            editor.apply()
        }
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param context
     * @param key
     * @param defaultObject
     * @return
     */
    fun getParam(context: Context?, key: String?, defaultObject: Any?): Any? {
        if (defaultObject != null) {
            val type = defaultObject.javaClass.simpleName
            val sp = context?.getSharedPreferences(mPrefsName, Context.MODE_PRIVATE)
            if ("String" == type) {
                return sp?.getString(key, defaultObject as String?)
            } else if ("Integer" == type) {
                return sp?.getInt(key, (defaultObject as Int?)!!)
            } else if ("Boolean" == type) {
                return sp?.getBoolean(key, (defaultObject as Boolean?)!!)
            } else if ("Float" == type) {
                return sp?.getFloat(key, (defaultObject as Float?)!!)
            } else if ("Long" == type) {
                return sp?.getLong(key, (defaultObject as Long?)!!)
            }
        }
        return null
    }

    fun removeParam(context: Context?, key: String?) {
        val sp = context?.getSharedPreferences(mPrefsName, Context.MODE_PRIVATE)
        val editor = sp?.edit()
        editor?.remove(key)
        editor?.apply()
    }

    /**
     * 清除缓存
     * @param context
     */
    fun clearSharePrefer(context: Context) {
        val sp = context.getSharedPreferences(mPrefsName, Context.MODE_PRIVATE)
        sp.edit().clear().apply()
    }

    companion object {
        private var instance: SharedPreferencesUtils? = null
        private var mPrefsName: String? = null
        @JvmStatic
        @Synchronized
        fun getInstance(prefsName: String?): SharedPreferencesUtils? {
            if (null == instance) {
                instance = SharedPreferencesUtils()
            }
            mPrefsName = prefsName
            return instance
        }

        const val PREF_NAME = "mendian"
        const val PREF_STATISTICS = "PREF_STATISTICS"
        @JvmStatic
        fun getString(context: Context, key: String?, defauleValue: String?): String? {
            val sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            //
            return sp.getString(key, defauleValue)
        }

        @JvmStatic
        fun setString(context: Context, key: String?, defauleValue: String?) {
            val sp = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            //
            sp.edit().putString(key, defauleValue).apply()
        }

        @JvmStatic
        fun getString(
            context: Context,
            name: String?,
            key: String?,
            defauleValue: String?
        ): String? {
            val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
            //
            return sp.getString(key, defauleValue)
        }

        @JvmStatic
        fun setString(context: Context, name: String?, key: String?, defauleValue: String?) {
            val sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
            //
            sp.edit().putString(key, defauleValue).apply()
        }
    }
}